package com.example.planetexploder.module.edittext;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.planetexploder.R;
import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.example.planetexploder.module.book.View_Book;
import com.example.planetexploder.module.firstpage.View_FirstPage;

/**
 * 文本编辑区域
 * @author lxx
 * 2019.10.29
 */
public class View_EditText extends AppCompatActivity {

    private Presenter_EditText presenter;
    private BookInfo bookInfo;
    private ItemInfo itemInfo;
    private EditText editText;
    private boolean trueActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textedit);
        //获取用于填写记事本信息的编辑框组件
        editText = findViewById(R.id.EditText);

        //取得启动该Activity的Intent对象
        Intent intent = getIntent();
        //取出Intent中附加的数据
        itemInfo = (ItemInfo) intent.getSerializableExtra("ItemInfo");
        bookInfo = (BookInfo) intent.getSerializableExtra("BookInfo");
        trueActivity = (boolean) intent.getSerializableExtra("Mark");

        presenter = new Presenter_EditText(View_EditText.this, bookInfo, itemInfo);
        //显示字符串到EditText中
        try {
            editText.setText(presenter.get_Text());
        } catch (Exception e) {
            e.printStackTrace();
        }
        /**
         * 实现返回arrow(<-)图标
         * 修改日期：2019.10.19
         */
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    /**
     * 功能：重写onCreateOptionMenu()方法来添加一个选项菜单
     * 修改日期：2019.10.19
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_edittext,menu);
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 功能：重写onOptionsItemSelected()方法，当选择保存按钮时，做出响应
     * 修改日期：2019.10.19
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == android.R.id.home) {
            finish();
            if(trueActivity){
                Intent intent = new Intent(View_EditText.this, View_FirstPage.class);
                startActivity(intent);
            }
            else{
                Intent intent = new Intent(View_EditText.this, View_Book.class);
                intent.putExtra("BookInfo", bookInfo);
                startActivity(intent);
            }
            return true;
        }
        else if(item.getItemId()==R.id.save_EditText){
               EditText editText = findViewById(R.id.EditText); //获取用于填写记事本信息的编辑框组件
                String text= editText.getText().toString(); //获取文本信息
                try {
                    presenter.save_Text(text);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Toast.makeText(View_EditText.this,"保存成功",Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }
}
